window.onload=function(){
    var parentWindow=window.parent;
    var footerBtn=parentWindow.document.querySelector(".footerBtn");
    var iframe=parentWindow.document.getElementsByTagName("iframe")[0];
    var selfchoose1 = document.querySelector(".selfchoose1");
    var selfchoose2 = document.querySelector(".selfchoose2");
    var addSelf = document.querySelector(".addSelf");
    var list_li = addSelf.children;
    var sortable = new Sortable(addSelf, {
        ghostClass: "bgColor",
        onEnd: function (event) {
            var arr=[];
            //自选拖拽效果未完成
            store.get("self_list").forEach(function(item,index){
                if(index!==event.newIndex&&index!==event.oldIndex){
                    arr.push(item);
                    console.log(item);
                }else if(index===event.newIndex&&index!==event.oldIndex){
                    arr.push(store.get("self_list")[event.newIndex]);
                    arr.push(store.get("self_list")[event.oldIndex]);
                    console.log(store.get("self_list")[event.oldIndex]);
                    console.log(store.get("self_list")[event.newIndex]);
                }
                // else if(index==event.oldIndex){
                    // arr.push(arr[event.oldIndex]);
                // }
            });
            console.log(arr);
            store.set("self_list",arr);
            arr=[];
        }
    });

    //搜索数据功能模块
    var input1 = document.getElementsByClassName("input1")[0];
    // console.log(input1);
    var fontSearch = document.getElementById("fontSearch");
    var inputSearch = document.getElementsByClassName("inputSearch")[0];
    //点击股票自选页面的搜索框触发事件
    // inputSearch.onclick = function () {
    //    selfchoose1.style.display="none";
    //    selfchoose2.style.display="block";
       //console.log(location.hash);
    //    location.hash="#inputSearch";
    //    changeStyle();
    // }

    //修改样式
    function changeStyle() {
        fontSearch.style.display = "none";
        input1.placeholder = "请输入股票";
        input1.style.textIndent = "0.227rem";
        input1.focus();
    }
    input1.onfocus=function(){
        changeStyle();
    }


    input1.onblur=function(){
        if (input1.value == "") {
            fontSearch.style.display = "block";
            input1.placeholder = "搜股票名称/股票代码";
            input1.style.textIndent = "1.727rem";
        }
    }


    //获取热搜数据
    ajax({
        type: 'get',
        url: 'http://127.0.0.1:8000/getHotStock',
        success: function (res) {
            HotStock.showHotStock(res.data.stock, ".hotStock");
            changeStyle();
        }
    });



    //热搜构造函数
    function HotStock(data, index) {
        this.li = document.createElement("li");
        this.li.setAttribute("data-index", index);
        var ul = document.createElement("ul");
        var li1 = document.createElement("li");
        var li2 = document.createElement("li");
        li2.className = "stock_Name";
        var li3 = document.createElement("li");
        li3.className = "stock_Code";
        var li4 = document.createElement("li");
        var p = document.createElement("p");
        li1.innerText = index + 1;
        li2.innerText = data[1];
        ajax({
            type: 'get',
            url: 'http://127.0.0.1:8000/stock_info',
            data: {
                secu_code: data[0]
            },
            success: function (res) {
                if (res.data.change * 100 < 0) {
                    li3.style.color = "#009a00";
                }
                li3.innerText = (res.data.change * 100).toFixed(2) + "%";
            }
        });
        p.innerText = "加自选";
        p.className = "addChoose";
        li4.appendChild(p);
        ul.appendChild(li1);
        ul.appendChild(li2);
        ul.appendChild(li3);
        ul.appendChild(li4);
        this.li.appendChild(ul);
    }
    //热搜函数
    HotStock.showHotStock = function (data, className) {
        var ul = document.querySelector(className);
        var self = this;
        data.forEach(function (item, index) {
            if (index < 4) {
                ul.appendChild(new self(item, index).li);
            }
        });
    }

    var card1 = document.getElementsByClassName("card1")[0];
    var card2 = document.getElementsByClassName("card2")[0];


    input1.onchange=function(){
        card1.style.display = "none";
        card2.style.display = "block";
        if (input1.value == "") {
            card1.style.display = "block";
            card2.style.display = "none";
            card2.innerHTML = "";
        }
        // setTimeout(function () {
        // if (flag) {
        ajax({
            type: 'get',
            url: "http://127.0.0.1:8000/search",
            data: {
                key: input1.value
            },
            success: function (res) {
                SearchData.showSearchData(res, ".card2");
            }
        });
        // }
        // }, 0)
    }



    //搜索数据构造函数
    function SearchData(data) {
        this.li = document.createElement("li");
        this.li.className = "clickLink";
        var ul = document.createElement("ul");
        var li1 = document.createElement("li");
        var li2 = document.createElement("li");
        ajax({
            type: 'get',
            url: 'http://127.0.0.1:8000/stock_info',
            data: {
                secu_code: "sh" + data.c
            },
            success: function (res) {
                li2.innerText = res.data.last_px;
                if (res.data.change * 100 > 0) {
                    li3.style.color = "#d6423a";
                } else if (res.data.change * 100 == 0) {
                    li3.style.color = "black";
                }
                li3.innerText = (res.data.change * 100).toFixed(2) + "%";
            }
        });
        var li3 = document.createElement("li");
        var li4 = document.createElement("li");
        var p1 = document.createElement("p");
        var p2 = document.createElement("p");
        //搜索的文字变蓝色
        var strIndex1 = data.n.indexOf(input1.value);
        if (strIndex1 != -1) {
            var startStr1 = data.n.substr(0, strIndex1);
            var newStr1 = data.n.substr(strIndex1, input1.value.length);
            var endStr1 = data.n.substr(strIndex1 + input1.value.length);
            p1.innerHTML = `${startStr1}<label class="colorStyle">${newStr1}</label>${endStr1}`;
            p2.innerText = data.c;
        } else if (data.c.indexOf(input1.value) != -1) {
            var strIndex2 = data.c.indexOf(input1.value);
            var startStr2 = data.c.substr(0, strIndex2);
            var newStr2 = data.c.substr(strIndex2, input1.value.length);
            var endStr2 = data.c.substr(strIndex2 + input1.value.length);
            p1.innerText = data.n;
            p2.innerHTML = `${startStr2}<label class="colorStyle">${newStr2}</label>${endStr2}`;
        }
        var p3 = document.createElement("p");
        p3.className = "addChoose";
        p3.innerText = "加自选";
        li1.appendChild(p1);
        li1.appendChild(p2);
        li4.appendChild(p3);
        ul.appendChild(li1);
        ul.appendChild(li2);
        ul.appendChild(li3);
        ul.appendChild(li4);
        this.li.appendChild(ul);
    }






     //搜索数据函数
    SearchData.showSearchData = function (data, className) {
        var ul = document.querySelector(className);
        var self = this;
        if (data.length === 0) {
            var li = document.createElement("li");
            li.innerText = "找不到任何匹配内容";
            li.setAttribute("style", "color:#c5c5c5;font-size:0.197rem;text-align:center");
            ul.appendChild(li);
        } else {
            if (ul.firstElementChild) {
                ul.removeChild(ul.firstElementChild);
            }
            data.forEach(function (item) {
                ul.appendChild(new self(item).li);
            });
        }
    }




    var fromHotSearch;
    var mask = document.querySelector('.mask');
    //添加自选效果
    var hotStock = document.querySelector(".hotStock");
    // 要点击的元素是后来添加进来的，所以需要进行事件时刻监听
    //热搜榜添加到自选页面
    hotStock.addEventListener("click", function (event) {
        var target = event.target || event.srcElement;
        if (target.className == "addChoose") {
            var stoUpDownNum = target.parentNode.parentNode.querySelector(".stock_Code").innerText;
            var stoName = target.parentNode.parentNode.querySelector(".stock_Name").innerText;
            ajax({
                type: 'get',
                url: "http://127.0.0.1:8000/search",
                data: {
                    key: stoName
                },
                success: function (res) {
                    ajax({
                        type: 'get',
                        url: 'http://127.0.0.1:8000/stock_info',
                        data: {
                            secu_code: "sh" + res[0].c
                        },
                        success: function (result) {
                            var obj = {
                                stockName: stoName,
                                stockCode: Number(res[0].c),
                                stoUpDownNum: stoUpDownNum,
                                newPrice: result.data.last_px
                            };
                            if (store.has("self_list")) {
                                var origin = store.get("self_list");
                                //判断当前本地数据内是否存在该数据
                                var result1 = origin.some(function (item) {
                                    return item.stockName === obj.stockName;
                                });
                                if (!result1) {
                                    origin.push(obj);
                                    store.set("self_list", origin);
                                } else {
                                    mask.style.display = "block";
                                }
                            } else {
                                store.set("self_list", [obj]);
                            }
                        }
                    });
                }
            });
        }else if(target.nodeName="LI"){
            sessionStorage.setItem("tabNum",2);
            fromHotSearch=true;
            stock_name=target.firstElementChild.children[1].innerText;
        
            ajax({
                type: 'get',
                url: "http://127.0.0.1:8000/search",
                data: {
                    key: stock_name
                },
                success: function (res) {
                    stock_code=res[0].c;
                    //标识是从热搜进入的行情页面
                    footerBtn.style.zIndex=-100;
                    selfchoose2.style.display="none";
                    transaction.style.display="block";
                    sessionStorage.setItem("stock_code",stock_code);
                    sessionStorage.setItem("stock_name",stock_name);
                    getData("sh" + stock_code); 
                }
            });
        }
    });


    function AddSelfInfo(data) {
        this.li = document.createElement("li");
        this.li.className = "list";
        var ul = document.createElement("ul");
        var li1 = document.createElement("li");
        var li2 = document.createElement("li");
        var li3 = document.createElement("li");
        var p1 = document.createElement("p");
        var p2 = document.createElement("p");
        p1.innerText = data.stockName;
        p2.innerText = data.stockCode;
        li1.appendChild(p1);
        li1.appendChild(p2);
        ul.appendChild(li1);
        li2.innerText = data.newPrice;
        li3.innerText = data.stoUpDownNum;
        ul.appendChild(li2);
        ul.appendChild(li3);
        this.li.appendChild(ul);
    }
    


    AddSelfInfo.showAddData = function (data, className) {
        var ul = document.querySelector(className);
        var self = this;
        data.forEach(function (item) {
            ul.appendChild(new self(item).li);
        })
        var div = document.createElement("div");
        div.className = "tipBtn";
        div.innerText = "添加自选股";
        selfchoose1.appendChild(div);
    }


    var addSelf = document.querySelector(".addSelf");
    //页面加载时判断添加自选页面的cookie是否为空
    if (store.get("self_list") !== undefined) {
        AddSelfInfo.showAddData(store.get("self_list"), ".addSelf");
    }


    selfchoose1.addEventListener("click",function(event){
        var target=event.target||event.srcElement;
        if(target.className==="tipBtn"){
            mask.style.display = "none";
            selfchoose2.style.display="block";
            selfchoose1.style.display="none";
        }
    })




    //弹窗显示
    var mask_cancel = document.querySelector(".mask_cancel");
    var mask_ok = document.querySelector(".mask_ok");
    mask_ok.onclick = function () {
        selfchoose1.style.display="block";
        selfchoose2.style.display="none";
        mask.style.display = "none";
        transaction.style.display="none";
    }
    mask_cancel.onclick = function () {
        mask.style.display = "none";
        selfchoose2.style.display="block";
        // card2.style.display="";
    }




    //搜索时添加到自选页面
    card2.addEventListener("click", function (event) {
        var target = event.target || event.srcElement;
        if (target.className === "addChoose") {
            var stockName = target.parentNode.parentNode.children[0].children[0].innerText;
            var stockCode = target.parentNode.parentNode.children[0].children[1].innerText;
            var newPrice = target.parentNode.parentNode.children[1].innerText;
            var percent = target.parentNode.parentNode.children[2].innerText;
            var obj = {
                stockName: stockName,
                stockCode: stockCode,
                stoUpDownNum: percent,
                newPrice: Number(newPrice)
            };
            if (store.has("self_list")) {
                var origin = store.get("self_list");
                //判断当前本地数据内是否存在该数据
                var result1 = origin.some(function (item) {
                    return item.stockName === obj.stockName;
                });
                if (!result1) {
                    origin.push(obj);
                    store.set("self_list", origin);
                } else {
                    mask.style.display = "block";
                }
            } else {
                store.set("self_list", [obj]);
            }
        }
    });

    var stock_code;
    var stock_name;
    var transaction=document.querySelector(".transaction");
     //用sessionStorage记录页面刷新时，当前页面是否是自选页面来的
    if(sessionStorage.getItem("tabLiNum")==="2"){
      location.hash="#inputSearch";
      stock_name=sessionStorage.getItem("stock_name");
      stock_code=sessionStorage.getItem("stock_code");
      getData("sh" + stock_code); 
    }
    
    
    if(sessionStorage.getItem("tabNum")==="2"){
      location.hash="#stockInfo";
      stock_name=sessionStorage.getItem("stock_name");
      stock_code=sessionStorage.getItem("stock_code");
      getData("sh" + stock_code); 
    }

   //判断是否是从主页面的排行榜跳转到股票行情页面
    if(parentWindow.dealData.get("fromHomePage")){
        selfchoose1.style.display="none";
        selfchoose2.style.display="none";
        transaction.style.display="block";
        stock_name=parentWindow.dealData.get("stock_name");
        stock_code=parentWindow.dealData.get("stock_code");
        sessionStorage.setItem("stock_name",stock_name);
        sessionStorage.setItem("stock_code",stock_code);    
        getData("sh" + stock_code); 
    }
    

    //从搜索进入股票行情页面
    card2.addEventListener("click",function(event){
         var target = event.target || event.srcElement;
         if (target.nodeName === "LI") {
            sessionStorage.setItem("tabNum",2);
            fromHotSearch=false;
            selfchoose2.style.display="none";
            transaction.style.display="block";
            stock_name=target.firstElementChild.firstElementChild.firstElementChild.innerText;
            stock_code=target.firstElementChild.firstElementChild.lastElementChild.innerText;
            sessionStorage.setItem("stock_code",stock_code);
            sessionStorage.setItem("stock_name",stock_name);
            getData("sh" + target.firstElementChild.firstElementChild.lastElementChild.innerText); 
        }
    });




    //搜索页面退出效果
    var leftBack1 = document.getElementsByClassName("leftBack1")[0];
    leftBack1.onclick = function () {
        sessionStorage.setItem("tabLiNum",1);
       if(parentWindow.dealData.get("fromHome")){
            iframe.src="../views/homePage.html";
       }else{
           iframe.src="../views/selfChoose.html";
       }
    }


    var leftBack2 = document.getElementsByClassName("leftBack2")[0];
    //行情页面退出效果
    leftBack2.onclick=function(){
        sessionStorage.setItem("tabNum",1);
        if(parentWindow.dealData.get("fromHomePage")){
            selfchoose1.style.display="block";
            selfchoose2.style.display="none";
            transaction.style.display="none";
            parentWindow.dealData.set("fromHomePage",false);
            parentWindow.location.hash="#homePage";
            parentWindow.document.getElementsByTagName("li")[1].className="";
            parentWindow.document.getElementsByTagName("li")[0].className="active";
        }else{
            if(fromHotSearch){
                selfchoose1.style.display="none";
                selfchoose2.style.display="block";
                card1.style.display="block";
                card2.style.display="none";
                transaction.style.display="none";
            }else{
                selfchoose1.style.display="none";
                selfchoose2.style.display="block";
                card1.style.display="none";
                card2.style.display="block";
                transaction.style.display="none";
            }
        }
    }


    //图表制作
    var last_px = document.getElementsByClassName("last_px")[0];
    var dom1 = document.getElementsByClassName("chart1")[0];
    var chart1 = echarts.init(dom1);
    var canvas = document.getElementsByTagName("canvas");


    //画图表
    function drawChart(arrData) {
        var markLineOpt = {
            //取消动画
            animation: false,
            //取消点击上去后y轴参考线会变白
            silent: true,
            symbol: "none",
            lineStyle: {
                color: "#eeeeee",
                type: "dashed"
            },
            data: [
                {
                    xAxis: '11:30',
                    lineStyle: {
                        // 局部辅助线的样式
                        type: "dashed",
                        color: "#eeeeee"
                    },
                },
                {
                    xAxis: '10:30',
                    lineStyle: {
                        // 局部辅助线的样式
                        type: "dashed",
                        color: "#eeeeee"
                    },
                },
                {
                    xAxis: '14:00',
                    lineStyle: {
                        // 局部辅助线的样式
                        type: "dashed",
                        color: "#eeeeee"
                    },
                },
                {
                    position: "center",
                    type: "average",
                    // yAxis: last_px.innerText,
                    lineStyle: {
                        // 局部辅助线的样式
                        type: "dashed",
                        color: "#eeeeee"
                    },
                },
            ],
        };
        var option = {
            //可以提示y轴的数据
            tooltip: {
                show: true,
                //开源节流
                //防抖函数，第一个参数是你要执行的函数，第二个参数是间隔时间
                formatter: _.debounce(function (point) {
                    //获取的是提示框的y轴数据
                    var val = point[0].value;
                    //获取所有的配置项
                    var originOpt = chart1.getOption();
                    //判断data最后一项的长度是否是5
                    if (originOpt.series[0].markLine.data.length === 5) {
                        //是5代表data最后一项存有之前的数据
                        //需要先删除之前的数据
                        originOpt.series[0].markLine.data.pop();
                    }
                    //把新获取的数据拼接成对象添加到data数组的最后一项去
                    originOpt.series[0].markLine.data.push({
                        //给y轴坐标重新赋值
                        yAxis: val,
                        lineStyle: {
                            // 局部辅助线的样式
                            type: "solid",
                            color: "#292929"
                        }
                    });
                    chart1.setOption(originOpt);
                }, 10)
                // 十字指针
                // axisPointer: {
                // type: 'cross',
                // label: {
                // 设置y轴标签背景颜色
                // backgroundColor: "#91b5ef"
                // },
                // 十字线的样式
                // crossStyle: {
                // color: "#292929",
                // type: "sold"
                // }
                // }
            },
            axisPointer: {
                type: "line",
                //共用一个坐标系
                link: {
                    xAxisIndex: 'all',
                }
            },
            grid: [
                //制作多个网格canvas写在一个盒子里面
                { top: 0, left: 0, right: 0, height: "60%" },
                { bottom: 0, left: 0, right: 0, height: "30%" }
            ],
            xAxis: [{
                //x轴上的十字指针
                axisPointer: {
                    type: 'line',
                    show: true,
                    label: {
                        //设置y轴标签背景颜色
                        backgroundColor: "#91b5ef"
                    },
                    //十字线的样式
                    lineStyle: {
                        color: "#292929",
                        type: "solid"
                    }
                },
                gridIndex: 0,
                type: "category",
                axisTick: {
                    show: false
                },
                data: timeArr.map(function (item, index) {
                    if (index === 0) {
                        return { value: item, textStyle: { align: "left" } };
                    } else if (index === 240) {
                        return { value: item, textStyle: { align: "right" } };
                    } else {
                        return { value: item, textStyle: { align: "center" } };
                    }
                }),
                axisLabel: {
                    interval: 59,
                    fontSize: 8,
                    color: "#999",
                    formatter: function (value, index) {
                        if (index == 0 || index == 60 || index == 120 || index == 180 || index == 240) {
                            return value.toString().replace(/(\d{1,2})(\d{2})/g, "$1:$2");
                        } else {
                            return "";
                        }
                    }
                },
                axisLine: {
                    lineStyle: {
                        color: "#eeeeee"
                    }
                }
            }, {
                gridIndex: 1,
                type: "category",
                axisTick: {
                    show: false
                },
                axisLabel: {
                    interval: 0,
                    formatter: function (value, index) {
                        if (index == 0 || index == 60 || index == 120 || index == 180 || index == 240) {
                            return value.toString().replace(/(\d{1,2})(\d{2})/g, "$1:$2");
                        } else {
                            return "";
                        }
                    }
                }
            }],
            yAxis: [{
                gridIndex: 0,
                axisLabel: {
                    //刻度内置
                    inside: true,
                    fontSize: "0.109rem",
                    interval: 0,
                    margin: -18,
                    formatter: function (value, index) {
                        if (index == 0 || index == 3 || index == 8) {
                            return value.toFixed(3);
                        } else {
                            return "";
                        }
                    },
                },
                position: "left",
                type: "value",
                show: true,
                splitLine: {
                    show: false
                },
                //获取的是y轴最小的值
                min: function (value) {
                    return value.min;
                },
                //获取的是y轴最大的值
                max: function (value) {
                    return value.max;
                }
            }, {
                gridIndex: 1,
                axisLabel: {
                    interval: 0,
                    show: false
                },
                position: "left",
                type: "value",
                show: true,
                splitLine: {
                    show: false
                },
                //获取的是y轴最小的值
                min: function (value) {
                    return value.min;
                },
                //获取的是y轴最大的值
                max: function (value) {
                    return value.max;
                }
            },],
            //配置折线
            series: [
                {
                    xAxisIndex: 0,
                    yAxisIndex: 0,
                    animation: false,
                    symbol: "none",
                    type: "line",
                    lineStyle: {
                        width: 1,
                        color: "#252525"
                    },
                    //控制折线的高亮样式
                    emphasis: {
                        lineStyle: {
                            width: 1
                        }
                    },
                    data: arrData.map(function (item, index) {
                        return item.last_px;
                    }),
                    //参考线
                    markLine: markLineOpt
                }, {
                    xAxisIndex: 1,
                    yAxisIndex: 1,
                    symbol: "none",
                    type: "bar",
                    lineStyle: {
                        color: "#252525"
                    },
                    data: arrData.map(function (item) {
                        return [item.minute, item.business_balance / item.business_amount];
                    }),
                    //参考线
                    markLine: markLineOpt
                }
            ],
        };
        chart1.setOption(option);
        //鼠标移出屏幕时触发的事件
        chart1.on("globalout", function () {
            var originOpt = chart1.getOption();
            if (originOpt.series[0].markLine.data.length === 5) {
                originOpt.series[0].markLine.data.pop();
            }
            chart1.setOption(originOpt);
        });
        canvas[0].style.width = "4.545rem";
        canvas[0].style.height = "3rem";
        //频繁改变窗口大小的时候再次执行画图，防止频繁执行
        window.onresize = _.debounce(function () {
            chart1.setOption(option);
        }, 30);//30毫秒执行一次，用于防抖
    }
    //ajax数据请求
    function getData(stockName) {
        //获取股票详细信息
        showStockInfo(stockName);
        //获取图表数据
        ajax({
            type: 'get',
            url: 'http://127.0.0.1:8000/timeline',
            data: {
                secu_code: stockName
            },
            success: function (res) {
                drawChart(res.data.line);
            }
        })
        //新闻数据请求
        ajax({
            type: 'get',
            url: 'http://127.0.0.1:8000/stock_news',
            success: function (res) {
                News.showNews(res.data, ".news");
            }
        });
    }
    function getNews() {
        //新闻数据请求
        ajax({
            type: 'get',
            url: 'http://127.0.0.1:8000/stock_news',
            success: function (res) {
                News.showNews(res.data, ".news");
            }
        });
    }
    //获取股票详细信息
    function showStockInfo(stockName) {
        var stock_Name = document.getElementsByClassName("stockName")[0];
        var change_px = document.getElementsByClassName("change_px")[0];
        var change = document.getElementsByClassName("change")[0];
        var high_px = document.getElementsByClassName("high_px")[0];
        var low_px = document.getElementsByClassName("low_px")[0];
        var open_px = document.getElementsByClassName("open_px")[0];
        var tr = document.getElementsByClassName("tr")[0];
        var business_amount = document.getElementsByClassName("business_amount")[0];
        var TotalShares = document.getElementsByClassName("TotalShares")[0];
        ajax({
            type: 'get',
            url: 'http://127.0.0.1:8000/stock_info',
            data: {
                secu_code: stockName
            },
            success: function (res) {
                stock_Name.innerText = stock_name + `(${stock_code})`;
                last_px.innerText = res.data.last_px.toFixed(2);
                if (res.data.change_px < 0) {
                    change_px.style.color = "#439629";
                }
                change_px.innerText = (res.data.change_px > 0) ? "+" + res.data.change_px : res.data.change_px;
                if (res.data.change * 100 < 0) {
                    change.style.color = "#439629";
                }
                change.innerText = (res.data.change * 100 > 0) ? "+" + (res.data.change * 100).toFixed(2) + "%" : (res.data.change * 100).toFixed(2) + "%";
                high_px.innerText = res.data.high_px.toFixed(2);
                low_px.innerText = res.data.low_px.toFixed(2);
                open_px.innerText = res.data.open_px.toFixed(2);
                tr.innerText = (res.data.tr * 100).toFixed(2) + "%";
                business_amount.innerText = (res.data.business_amount / 1000000).toFixed(1) + "万";
                TotalShares.innerText = (res.data.business_balance / 100000000).toFixed(1) + "亿";
            }
        });
    }
    //新闻数据显示
    function News(news) {
        this.li = document.createElement("li");
        var p = document.createElement("p");
        var ul = document.createElement("ul");
        var li1 = document.createElement("li");
        var li2 = document.createElement("li");
        p.innerText = news.title;
        li1.innerText = news.source;
        var nowDate = new Date();
        var date = dayjs(news.ctime * 1000).format('YYYY-MM-DD');
        var year = date.substring(0, 4);
        var mon = date.substring(5, 7);
        var day = date.substring(8, 10);
        var result;
        if (nowDate.getFullYear().toString() === year && nowDate.getMonth().toString() === mon && nowDate.getDate().toString() === day) {
            result = dayjs(news.ctime * 1000).format('HH:mm');
        } else {
            result = dayjs(news.ctime * 1000).format('MM-DD');
        }
        li2.innerText = result;
        this.li.appendChild(p);
        ul.appendChild(li1);
        ul.appendChild(li2);
        this.li.appendChild(ul);
    }
    News.showNews = function (data, className) {
        var ul = document.querySelector(className);
        var self = this;
        data.forEach(function (item, index) {
            if (index < 20) {
                ul.appendChild(new self(item).li);
            }
        });
        var li = document.createElement("li");
        li.className = "colorstyle";
        li.innerText = "加载更多";
        ul.appendChild(li);
        //加载数据
        var colorstyle = document.getElementsByClassName("colorstyle");
        colorstyle[0].onclick = function () {
            ul.removeChild(ul.lastElementChild);
            ajax({
                type: 'get',
                url: 'http://127.0.0.1:8000/stock_news',
                success: function (res) {
                    News.showNews(res.data, ".news");
                    var li = document.createElement("li");
                    li.className = "colorstyle";
                    li.innerText = "加载更多";
                }
            });
        }
    }


    //用来判断是否是从home页面进入搜索页面
    if(parentWindow.dealData.get("fromHome")){
        selfchoose1.style.display="none";
        selfchoose2.style.display="block";
        transaction.style.display="none";
    }




    //行情页面添加到自选列表
    var changeBtn = document.getElementsByClassName("changeBtn")[0];
    changeBtn.addEventListener("click", function (event) {
        var target = event.target || event.srcElement;
        if (target.className === "getSelfInfo") {
            var last_px = document.getElementsByClassName("last_px")[0];
            var change = document.getElementsByClassName("change")[0];
            var stockName = document.getElementsByClassName("stockName")[0];
            var startIndex = stockName.innerText.indexOf("(");
            var endIndex = stockName.innerText.indexOf(")");
            var stockCodeStr = stockName.innerText.substring(startIndex + 1, endIndex);
            var stockNameStr = stockName.innerText.substring(0, startIndex);
            var obj = {
                stockName: stockNameStr,
                stockCode: stockCodeStr,
                stoUpDownNum: change.innerText,
                newPrice: last_px.innerText
            };
            if (store.has("self_list")) {
                var origin = store.get("self_list");
                var result = origin.some(function (item) {
                    return item.stockName === obj.stockName;
                });
                if (!result) {
                    origin.push(obj);
                    store.set("self_list", origin);
                } else {
                    mask.style.display = "block";
                }
            } else {
                store.set("self_list", [obj]);
            }
        }
    });

}







//同一个页面进行切换效果
window.addEventListener("DOMContentLoaded",function(){
    var parentWindow=window.parent;
    var iframe=parentWindow.document.getElementsByTagName("iframe")[0];
    var selfchoose1 = document.querySelector(".selfchoose1");
    var selfchoose2 = document.querySelector(".selfchoose2");
    var transaction=document.querySelector(".transaction");
    //  搜索数据功能模块
    var input1 = document.getElementsByClassName("input1")[0];
// 
    // 获取当前页面的hash值，并且执行相应的回调函数
    function getSrc(option){
        var hash=location.hash.substring(1);
        option[hash]();
    }
// 
    // 函数功能：给window同时绑定多个事件
    function on(events,callback){
        var event=events.split(" ");
        for(var o in event){
            // for...in可以遍历数组
            window.addEventListener(event[o],callback);
        }
    }
    function changeStyle() {
        fontSearch.style.display = "none";
        input1.placeholder = "请输入股票";
        input1.style.textIndent = "0.227rem";
        input1.focus();
    }

    on("load hashchange",function(){
    getSrc({
        //首页切换
        "inputSearch":function(){
            parentWindow.dealData.set("inputSearch",false);
            selfchoose1.style.display="none";
            selfchoose2.style.display="block";
            sessionStorage.setItem("tabLiNum",2);
            console.log(location.hash);
            location.hash="#selfChoose1";
            changeStyle();
        },
        //自选切换
        "selfChoose1":function(){
            selfchoose1.style.display="none";
            selfchoose2.style.display="block";
        },
        "":function(){
        },
        "stockInfo":function(){
            selfchoose1.style.display="none";
            selfchoose2.style.display="none";
            transaction.style.display="block";
        }
    });
})
});